package com.sdyc.ndmp.cls.engine;


import com.sdyc.ndmp.cls.dtd.Condition;
import com.sdyc.ndmp.cls.model.SonW;
import com.sdyc.ndmp.cls.dtd.KeywordCondition;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *     构造 Filter Factory
 * </p>
 */
public final class FilterFactory {


    public static Filter newInstance(final Condition condition) {
        return new ContainsKeywordFilter(condition);
    }


    public static Filter newInstance(final KeywordCondition condition) {
        final PrimeFilter primeFilter = new PrimeFilter(condition);
        final KeywordVectorFilter keywordVectorFilter = KeywordVectorFilter.getFilter(condition);
        return new Filter() {

            @Override
            public boolean accept(DataKeywords post) {
                //主条件不通过, 则返回 false
                if (!primeFilter.accept(post)) {
                    return false;
                }

                /*
                 * 没有附加关键字字距, 直接返回 true
                 */
                if(keywordVectorFilter == null) {
                    return true;
                }
                Map<Match, Set<String>> primeKeywords = primeFilter.primeKeywords();

                /*
                 * 主条件是 NOT, 不能有附加条件
                 */
                if(primeKeywords.isEmpty()) {
                    return true;
                }

                //主条件通过了, 要看Keyword 词距是否符合
                return keywordVectorFilter.accept(post, primeKeywords);
            }
        };
    }

    public static Filter singleFilter(Match matchType, Set<String> words) {
        final PrimeFilter primeFilter = new PrimeFilter(matchType, words);
        return new Filter() {

            @Override
            public boolean accept(DataKeywords post) {
                //主条件不通过, 则返回 false
                return primeFilter.accept(post);
            }
        };
    }

    public static Filter newInstanceSameScope(final KeywordCondition condition, SonW sonW) {
        //查找范围不同，不需要处理词间距
        final PrimeFilter primeFilter = new PrimeFilter(condition);
        Set<KeywordVectorFilter> vectorFilters = new HashSet<KeywordVectorFilter>();
        vectorFilters.add(new MatchKeywordVectorFilter(sonW));
        final KeywordVectorFilter keywordVectorFilter = new AllSetKeywordVectorFilter(vectorFilters);
        return new Filter() {

            @Override
            public boolean accept(DataKeywords post) {
                //主条件不通过, 则返回 false
                if (!primeFilter.accept(post)) {
                    return false;
                }

                /*
                 * 没有附加关键字字距, 直接返回 true
                 */
                if(keywordVectorFilter == null) {
                    return true;
                }
                Map<Match, Set<String>> primeKeywords = primeFilter.primeKeywords();

                /*
                 * 主条件是 NOT, 不能有附加条件
                 */
                if(primeKeywords.isEmpty()) {
                    return true;
                }

                //主条件通过了, 要看Keyword 词距是否符合
                return keywordVectorFilter.accept(post, primeKeywords);
            }
        };
    }
}
